package ua.pp.shell.wka;
import java.util.Locale;

import ua.pp.shell.wka.DBAdapter.Types.Vocabulary;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.Toast;

public class VocabularyListActivity extends Activity {
    private DBAdapter dbAdapter = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //
        String languageToLoad = "ru";
        Locale locale = new Locale(languageToLoad);
        Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.locale = locale;
        getBaseContext().getResources().updateConfiguration(config,
        getBaseContext().getResources().getDisplayMetrics());
        //
        setContentView(R.layout.vocabularies_activity);
        ListView list = (ListView)findViewById(R.id.list);
        dbAdapter = new DBAdapter(this);
        VocabularyListAdapter listAdapter = new VocabularyListAdapter(this, 1, dbAdapter.GetVocabularies());
        list.setAdapter(listAdapter);  
    }
   
        
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }
    
    @Override 
    public boolean onOptionsItemSelected(MenuItem item) {
    	Settings settings;
    	switch (item.getItemId()) {
        	case R.id.add:
        		Intent intent = new Intent(getApplicationContext(), AEVocabularyActivity.class);
        		startActivityForResult(intent, App.activities.AE_VOCABULARY.ordinal());
        		break;
        	case R.id.remove:
        		AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
                alertbox.setMessage(R.string.delete_confirmation);
                alertbox.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface arg0, int arg1) {
                    	GetListAdapter().DeleteSelected();
                    }
                });
                alertbox.setNegativeButton("No", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface arg0, int arg1) {
                        Toast.makeText(getApplicationContext(), "'No' button clicked", Toast.LENGTH_SHORT).show();
                    }
                });
                alertbox.show();
        		break;
        	case R.id.to_sd:
        		settings = new Settings(getApplicationContext());
        		settings.SaveToSD();
        		break;
        	case R.id.from_sd:
        		settings = new Settings(getApplicationContext());
        		settings.LoadFromSD();
        		break;
        	case R.id.quit:
        		this.finish();
        		break;
    	}
    	return true;
    }
    
    public VocabularyListAdapter GetListAdapter() {
    	ListView list = (ListView)findViewById(R.id.list);
		return (VocabularyListAdapter)list.getAdapter();
    }
    
    protected void onActivityResult(int request, int result, Intent data) {
    	switch(result) {
    		case RESULT_OK:
    			if (request == App.activities.AE_VOCABULARY.ordinal()) {
    				int id = data.getIntExtra("id", 0);
        			boolean add_new = data.getBooleanExtra("add_new", false);
        			Vocabulary vocabulary = dbAdapter.GetVocabulary(id);
                    if ( add_new == false ) {
                    	GetListAdapter().UpdateVocabulary(vocabulary);
                    	Toast.makeText(this, String.format(getString(R.string.vocabulary_updated), vocabulary.title), 2000).show();
                    } else if ( add_new == true ){
                    	GetListAdapter().AddVocabulary(vocabulary);
                    	Toast.makeText(this, String.format(getString(R.string.vocabulary_created), vocabulary.title), 2000).show();
                    }
    			}      
    			break;
    		case RESULT_CANCELED:
    			break;
    	}
    }
}